package com.ruoyi.web.controller.purchaseplan;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.MaterialsRequisitiones;
import com.ruoyi.service.IMaterialsRequisitionesService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/ruoyi/purchaseplanmoney")
public class purchaseplanMoneyController extends BaseController {
    private String prefix = "ruoyi/purchaseplan";
    @Autowired
    private IMaterialsRequisitionesService materialsRequisitionesService;
    @RequiresPermissions("ruoyi:purchaseplanmoney:view")
    @GetMapping()
    public String requisitiones()
    {
        return prefix + "/purchaseplanmoney";
    }

    /**
     * 查询总项目对应的材料申请单
     */
    @RequiresPermissions("ruoyi:purchaseplanmoney:lists")
    @PostMapping("/lists")
    @ResponseBody
    public TableDataInfo lists(Long mId)
    {
        startPage();
        List<MaterialsRequisitiones> list = materialsRequisitionesService.selectLists(mId,4L);
        return getDataTable(list);
    }

    @GetMapping("/price/{mMid}")
    public String add(@PathVariable("mMid") Long mMid, ModelMap map)
    {
        MaterialsRequisitiones materialsRequisitiones = materialsRequisitionesService.selectMaterialsRequisitionesById(mMid);
        map.put("materialsRequisitiones", materialsRequisitiones);
        return prefix + "/prices";
    }

    /**
     * 修改保存材料申请单
     */
    @RequiresPermissions("ruoyi:purchaseplanmoney:edit")
    @Log(title = "材料申请单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MaterialsRequisitiones materialsRequisitiones)
    {
        MaterialsRequisitiones materialsRequisitiones1 = materialsRequisitionesService.selectMaterialsRequisitionesById(materialsRequisitiones.getmMid());
        Long mSum = materialsRequisitiones.getmBuy()+materialsRequisitiones1.getmBuy();
        System.out.println(mSum);
        if(mSum>materialsRequisitiones1.getMatNumbers()){
            return toAjax(0);
        }else{
            materialsRequisitiones.setmUnbuy(materialsRequisitiones1.getMatNumbers()-mSum);
            materialsRequisitiones.setmBuy(mSum);
            if(mSum==materialsRequisitiones1.getMatNumbers()){
                materialsRequisitiones.setmIsover(1L);
            }
        }
        return toAjax(materialsRequisitionesService.updateMaterialsRequisitiones(materialsRequisitiones));
    }
}
